#include "graphic.h"
#include "sget.h"
#include "callbacks.h"
#include "support.h"

#define GLADE_HOOKUP_OBJECT(component,widget,name) \
   g_object_set_data_full (G_OBJECT (component), name, \
      gtk_widget_ref (widget), (GDestroyNotify) gtk_widget_unref)

#define GLADE_HOOKUP_OBJECT_NO_REF(component,widget,name) \
   g_object_set_data (G_OBJECT (component), name, widget)

GtkWidget* create_main (void) {

   GtkWidget *main;
   GtkWidget *vbox1;
   GtkWidget *menubar;
   GtkWidget *menuitem4;
   GtkWidget *menu4;
   GtkWidget *new1;
   GtkWidget *open1;
   GtkWidget *save1;
   GtkWidget *save_as1;
   GtkWidget *separatormenuitem1;
   GtkWidget *quit1;
   GtkWidget *menuitem5;
   GtkWidget *menu5;
   GtkWidget *cut1;
   GtkWidget *copy1;
   GtkWidget *paste1;
   GtkWidget *delete1;
   GtkWidget *menuitem6;
   GtkWidget *menu6;
   GtkWidget *menuitem7;
   GtkWidget *menu7;
   GtkWidget *about1;
   GtkWidget *vbox2;
   GtkWidget *vbox3;
   GtkWidget *vbox4;
   GtkWidget *toolbar;
   GtkIconSize tmp_toolbar_icon_size;
   GtkWidget *addbutton;
   GtkWidget *toolbutton2;
   GtkWidget *toolbutton3;
   GtkWidget *toolbutton_cancel;
   GtkWidget *toolbutton5;
   GtkWidget *vbox5;
   GtkWidget *vbox6;
   GtkWidget *vbox7;
   GtkWidget *scrolledwindow1;
   GtkWidget *treeview;
   GtkWidget *drawingarea1;
   GtkWidget *statusbar1;
   GtkTreeModel *model;
   GtkCellRenderer *renderer;
   GtkListStore *store;
   GtkTreeIter iter;
   GtkAccelGroup *accel_group;

   accel_group = gtk_accel_group_new ();

   main = gtk_window_new (GTK_WINDOW_TOPLEVEL);
   gtk_widget_set_size_request (main, 700, 500);
   gtk_window_set_title (GTK_WINDOW (main), SGET_MAIN_NAME);
   gtk_window_set_position (GTK_WINDOW (main), GTK_WIN_POS_CENTER);

   vbox1 = gtk_vbox_new (FALSE, 0);
   gtk_widget_show (vbox1);
   gtk_container_add (GTK_CONTAINER (main), vbox1);

   menubar = gtk_menu_bar_new ();
   gtk_widget_show (menubar);
   gtk_box_pack_start (GTK_BOX (vbox1), menubar, FALSE, FALSE, 0);

   menuitem4 = gtk_menu_item_new_with_mnemonic("_File");
   gtk_widget_show (menuitem4);
   gtk_container_add (GTK_CONTAINER (menubar), menuitem4);

   menu4 = gtk_menu_new ();
   gtk_menu_item_set_submenu (GTK_MENU_ITEM (menuitem4), menu4);

   new1 = gtk_image_menu_item_new_from_stock ("gtk-new", accel_group);
   gtk_widget_show (new1);
   gtk_container_add (GTK_CONTAINER (menu4), new1);

   open1 = gtk_image_menu_item_new_from_stock ("gtk-open", accel_group);
   gtk_widget_show (open1);
   gtk_container_add (GTK_CONTAINER (menu4), open1);

   save1 = gtk_image_menu_item_new_from_stock ("gtk-save", accel_group);
   gtk_widget_show (save1);
   gtk_container_add (GTK_CONTAINER (menu4), save1);

   save_as1 = gtk_image_menu_item_new_from_stock ("gtk-save-as", accel_group);
   gtk_widget_show (save_as1);
   gtk_container_add (GTK_CONTAINER (menu4), save_as1);

   separatormenuitem1 = gtk_separator_menu_item_new ();
   gtk_widget_show (separatormenuitem1);
   gtk_container_add (GTK_CONTAINER (menu4), separatormenuitem1);
   gtk_widget_set_sensitive (separatormenuitem1, FALSE);

   quit1 = gtk_image_menu_item_new_from_stock ("gtk-quit", accel_group);
   gtk_widget_show (quit1);
   gtk_container_add (GTK_CONTAINER (menu4), quit1);

   menuitem5 = gtk_menu_item_new_with_mnemonic (_("_Edit"));
   gtk_widget_show (menuitem5);
   gtk_container_add (GTK_CONTAINER (menubar), menuitem5);

   menu5 = gtk_menu_new ();
   gtk_menu_item_set_submenu (GTK_MENU_ITEM (menuitem5), menu5);

   cut1 = gtk_image_menu_item_new_from_stock ("gtk-cut", accel_group);
   gtk_widget_show (cut1);
   gtk_container_add (GTK_CONTAINER (menu5), cut1);

   copy1 = gtk_image_menu_item_new_from_stock ("gtk-copy", accel_group);
   gtk_widget_show (copy1);
   gtk_container_add (GTK_CONTAINER (menu5), copy1);

   paste1 = gtk_image_menu_item_new_from_stock ("gtk-paste", accel_group);
   gtk_widget_show (paste1);
   gtk_container_add (GTK_CONTAINER (menu5), paste1);

   delete1 = gtk_image_menu_item_new_from_stock ("gtk-delete", accel_group);
   gtk_widget_show (delete1);
   gtk_container_add (GTK_CONTAINER (menu5), delete1);

   menuitem6 = gtk_menu_item_new_with_mnemonic (_("_View"));
   gtk_widget_show (menuitem6);
   gtk_container_add (GTK_CONTAINER (menubar), menuitem6);

   menu6 = gtk_menu_new ();
   gtk_menu_item_set_submenu (GTK_MENU_ITEM (menuitem6), menu6);

   menuitem7 = gtk_menu_item_new_with_mnemonic (_("_Help"));
   gtk_widget_show (menuitem7);
   gtk_container_add (GTK_CONTAINER (menubar), menuitem7);

   menu7 = gtk_menu_new ();
   gtk_menu_item_set_submenu (GTK_MENU_ITEM (menuitem7), menu7);

   about1 = gtk_menu_item_new_with_mnemonic (_("_About"));
   gtk_widget_show (about1);
   gtk_container_add (GTK_CONTAINER (menu7), about1);

   vbox2 = gtk_vbox_new (FALSE, 0);
   gtk_widget_show (vbox2);
   gtk_box_pack_start (GTK_BOX (vbox1), vbox2, TRUE, TRUE, 0);

   vbox3 = gtk_vbox_new (FALSE, 0);
   gtk_widget_show (vbox3);
   gtk_box_pack_start (GTK_BOX (vbox2), vbox3, TRUE, TRUE, 0);

   vbox4 = gtk_vbox_new (FALSE, 0);
   gtk_widget_show (vbox4);
   gtk_box_pack_start (GTK_BOX (vbox3), vbox4, TRUE, TRUE, 0);

   toolbar = gtk_toolbar_new ();
   gtk_widget_show (toolbar);
   gtk_box_pack_start (GTK_BOX (vbox4), toolbar, FALSE, FALSE, 0);
   gtk_widget_set_size_request (toolbar, -1, 40);
   gtk_toolbar_set_style (GTK_TOOLBAR (toolbar), GTK_TOOLBAR_BOTH);
   tmp_toolbar_icon_size = gtk_toolbar_get_icon_size (GTK_TOOLBAR (toolbar));

   addbutton = (GtkWidget*) gtk_tool_button_new_from_stock ("gtk-add");
   gtk_widget_show (addbutton);
   gtk_container_add (GTK_CONTAINER (toolbar), addbutton);
   gtk_widget_set_size_request (addbutton, 50, -1);

   toolbutton2 = (GtkWidget*) gtk_tool_button_new_from_stock ("gtk-remove");
   gtk_widget_show (toolbutton2);
   gtk_container_add (GTK_CONTAINER (toolbar), toolbutton2);

   toolbutton3 = (GtkWidget*) gtk_tool_button_new_from_stock ("gtk-properties");
   gtk_widget_show (toolbutton3);
   gtk_container_add (GTK_CONTAINER (toolbar), toolbutton3);

   toolbutton_cancel = (GtkWidget*) gtk_tool_button_new_from_stock ("gtk-media-pause");
   gtk_widget_show (toolbutton_cancel);
   gtk_container_add (GTK_CONTAINER (toolbar), toolbutton_cancel);

   toolbutton5 = (GtkWidget*) gtk_tool_button_new_from_stock ("gtk-media-stop");
   gtk_widget_show (toolbutton5);
   gtk_container_add (GTK_CONTAINER (toolbar), toolbutton5);

   vbox5 = gtk_vbox_new (FALSE, 0);
   gtk_widget_show (vbox5);
   gtk_box_pack_start (GTK_BOX (vbox4), vbox5, TRUE, TRUE, 0);

   vbox6 = gtk_vbox_new (FALSE, 0);
   gtk_widget_show (vbox6);
   gtk_box_pack_start (GTK_BOX (vbox5), vbox6, TRUE, TRUE, 0);

   vbox7 = gtk_vbox_new (FALSE, 0);
   gtk_widget_show (vbox7);
   gtk_box_pack_start (GTK_BOX (vbox6), vbox7, TRUE, TRUE, 0);

   scrolledwindow1 = gtk_scrolled_window_new (NULL, NULL);
   gtk_widget_show (scrolledwindow1);
   gtk_box_pack_start (GTK_BOX (vbox7), scrolledwindow1, TRUE, TRUE, 0);
   gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolledwindow1), GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
   gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (scrolledwindow1), GTK_SHADOW_IN);

   treeview = gtk_tree_view_new ();
   gtk_widget_show (treeview);
   gtk_container_add (GTK_CONTAINER (scrolledwindow1), treeview);

   renderer = gtk_cell_renderer_text_new ();
   gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (treeview), -1, "File",   renderer, "text", 0, NULL);
   renderer = gtk_cell_renderer_text_new ();
   gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (treeview), -1, "%",   renderer, "text", 1, NULL);
   renderer = gtk_cell_renderer_text_new ();
   gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (treeview), -1, "Status",   renderer, "text", 2, NULL);

   store = gtk_list_store_new (3, G_TYPE_STRING, G_TYPE_UINT, G_TYPE_STRING);

   gtk_tree_view_set_model(GTK_TREE_VIEW(treeview), GTK_TREE_MODEL(store));

   drawingarea1 = gtk_drawing_area_new ();
   gtk_widget_show (drawingarea1);
   gtk_box_pack_start (GTK_BOX (vbox6), drawingarea1, FALSE, FALSE, 0);

   statusbar1 = gtk_statusbar_new ();
   gtk_widget_show (statusbar1);
   gtk_box_pack_start (GTK_BOX (vbox5), statusbar1, FALSE, FALSE, 0);

   g_signal_connect ((gpointer) main, "destroy", G_CALLBACK (gtk_main_quit), NULL);
   g_signal_connect ((gpointer) addbutton, "clicked", G_CALLBACK (on_addbutton_press), NULL);
   g_signal_connect ((gpointer) open1, "activate", G_CALLBACK (on_open1_activate), NULL);
   g_signal_connect ((gpointer) save1, "activate", G_CALLBACK (on_save1_activate), NULL);
   g_signal_connect ((gpointer) save_as1, "activate", G_CALLBACK (on_save_as1_activate), NULL);
   g_signal_connect ((gpointer) quit1, "activate", G_CALLBACK (on_quit1_activate), NULL);
   g_signal_connect ((gpointer) cut1, "activate", G_CALLBACK (on_cut1_activate), NULL);
   g_signal_connect ((gpointer) copy1, "activate", G_CALLBACK (on_copy1_activate), NULL);
   g_signal_connect ((gpointer) paste1, "activate", G_CALLBACK (on_paste1_activate), NULL);
   g_signal_connect ((gpointer) delete1, "activate", G_CALLBACK (on_delete1_activate), NULL);
   g_signal_connect ((gpointer) about1, "activate", G_CALLBACK (on_about1_activate), NULL);
   g_signal_connect ((gpointer) treeview, "realize", G_CALLBACK (on_treeview_realize), store );

   /* Store pointers to all widgets, for use by lookup_widget(). */
   GLADE_HOOKUP_OBJECT_NO_REF (main, main, "main");
   GLADE_HOOKUP_OBJECT (main, vbox1, "vbox1");
   GLADE_HOOKUP_OBJECT (main, menubar, "menubar");
   GLADE_HOOKUP_OBJECT (main, menuitem4, "menuitem4");
   GLADE_HOOKUP_OBJECT (main, menu4, "menu4");
   GLADE_HOOKUP_OBJECT (main, new1, "new1");
   GLADE_HOOKUP_OBJECT (main, open1, "open1");
   GLADE_HOOKUP_OBJECT (main, save1, "save1");
   GLADE_HOOKUP_OBJECT (main, save_as1, "save_as1");
   GLADE_HOOKUP_OBJECT (main, separatormenuitem1, "separatormenuitem1");
   GLADE_HOOKUP_OBJECT (main, quit1, "quit1");
   GLADE_HOOKUP_OBJECT (main, menuitem5, "menuitem5");
   GLADE_HOOKUP_OBJECT (main, menu5, "menu5");
   GLADE_HOOKUP_OBJECT (main, cut1, "cut1");
   GLADE_HOOKUP_OBJECT (main, copy1, "copy1");
   GLADE_HOOKUP_OBJECT (main, paste1, "paste1");
   GLADE_HOOKUP_OBJECT (main, delete1, "delete1");
   GLADE_HOOKUP_OBJECT (main, menuitem6, "menuitem6");
   GLADE_HOOKUP_OBJECT (main, menu6, "menu6");
   GLADE_HOOKUP_OBJECT (main, menuitem7, "menuitem7");
   GLADE_HOOKUP_OBJECT (main, menu7, "menu7");
   GLADE_HOOKUP_OBJECT (main, about1, "about1");
   GLADE_HOOKUP_OBJECT (main, vbox2, "vbox2");
   GLADE_HOOKUP_OBJECT (main, vbox3, "vbox3");
   GLADE_HOOKUP_OBJECT (main, vbox4, "vbox4");
   GLADE_HOOKUP_OBJECT (main, toolbar, "toolbar");
   GLADE_HOOKUP_OBJECT (main, addbutton, "addbutton");
   GLADE_HOOKUP_OBJECT (main, toolbutton2, "toolbutton2");
   GLADE_HOOKUP_OBJECT (main, toolbutton3, "toolbutton3");
   GLADE_HOOKUP_OBJECT (main, toolbutton_cancel, "toolbutton4");
   GLADE_HOOKUP_OBJECT (main, toolbutton5, "toolbutton5");
   GLADE_HOOKUP_OBJECT (main, vbox5, "vbox5");
   GLADE_HOOKUP_OBJECT (main, vbox6, "vbox6");
   GLADE_HOOKUP_OBJECT (main, vbox7, "vbox7");
   GLADE_HOOKUP_OBJECT (main, scrolledwindow1, "scrolledwindow1");
   GLADE_HOOKUP_OBJECT (main, treeview, "treeview");
   GLADE_HOOKUP_OBJECT (main, drawingarea1, "drawingarea1");
   GLADE_HOOKUP_OBJECT (main, statusbar1, "statusbar1");

   gtk_window_add_accel_group (GTK_WINDOW (main), accel_group);

   return main;

}

GtkWidget* create_download (void) {

  GtkWidget *download;
  GtkWidget *vbox1;
  GtkWidget *notebook1;
  GtkWidget *fixed1;
  GtkWidget *urlentry;
  GtkWidget *label5;
  GtkWidget *label1;
  GtkWidget *empty_notebook_page;
  GtkWidget *label2;
  GtkWidget *hbuttonbox1;
  GtkWidget *button_cancel;
  GtkWidget *alignment2;
  GtkWidget *hbox2;
  GtkWidget *image2;
  GtkWidget *label4;
  GtkWidget *button_ok;
  GtkWidget *alignment1;
  GtkWidget *hbox1;
  GtkWidget *image1;
  GtkWidget *label3;

  download = gtk_window_new (GTK_WINDOW_TOPLEVEL);
  gtk_widget_set_size_request (download, 450, 450);
  gtk_window_set_position (GTK_WINDOW (download), GTK_WIN_POS_CENTER);
  gtk_window_set_title (GTK_WINDOW (download), _("New Download"));

  vbox1 = gtk_vbox_new (FALSE, 0);
  gtk_widget_show (vbox1);
  gtk_container_add (GTK_CONTAINER (download), vbox1);

  notebook1 = gtk_notebook_new ();
  gtk_widget_show (notebook1);
  gtk_box_pack_start (GTK_BOX (vbox1), notebook1, TRUE, TRUE, 0);

  fixed1 = gtk_fixed_new ();
  gtk_widget_show (fixed1);
  gtk_container_add (GTK_CONTAINER (notebook1), fixed1);

  urlentry = gtk_entry_new ();
  gtk_widget_show (urlentry);
  gtk_fixed_put (GTK_FIXED (fixed1), urlentry, 64, 16);
  gtk_widget_set_size_request (urlentry, 360, 24);
  gtk_entry_set_invisible_char (GTK_ENTRY (urlentry), 9679);

  label5 = gtk_label_new (_("URL:"));
  gtk_widget_show (label5);
  gtk_fixed_put (GTK_FIXED (fixed1), label5, 24, 16);
  gtk_widget_set_size_request (label5, 40, 24);
  gtk_misc_set_alignment (GTK_MISC (label5), 0.5, 0.6);

  label1 = gtk_label_new (_("Main"));
  gtk_widget_show (label1);
  gtk_notebook_set_tab_label (GTK_NOTEBOOK (notebook1), gtk_notebook_get_nth_page (GTK_NOTEBOOK (notebook1), 0), label1);

  empty_notebook_page = gtk_vbox_new (FALSE, 0);
  gtk_widget_show (empty_notebook_page);
  gtk_container_add (GTK_CONTAINER (notebook1), empty_notebook_page);

  label2 = gtk_label_new (_("soon"));
  gtk_widget_show (label2);
  gtk_notebook_set_tab_label (GTK_NOTEBOOK (notebook1), gtk_notebook_get_nth_page (GTK_NOTEBOOK (notebook1), 1), label2);

  hbuttonbox1 = gtk_hbutton_box_new ();
  gtk_widget_show (hbuttonbox1);
  gtk_box_pack_start (GTK_BOX (vbox1), hbuttonbox1, FALSE, TRUE, 0);
  gtk_widget_set_size_request (hbuttonbox1, -1, 40);
  gtk_container_set_border_width (GTK_CONTAINER (hbuttonbox1), 5);

  button_cancel = gtk_button_new ();
  gtk_widget_show (button_cancel);
  gtk_container_add (GTK_CONTAINER (hbuttonbox1), button_cancel);
  gtk_widget_set_size_request (button_cancel, 65, 30);
  GTK_WIDGET_SET_FLAGS (button_cancel, GTK_CAN_DEFAULT);

  alignment2 = gtk_alignment_new (0.5, 0.5, 0, 0);
  gtk_widget_show (alignment2);
  gtk_container_add (GTK_CONTAINER (button_cancel), alignment2);

  hbox2 = gtk_hbox_new (FALSE, 2);
  gtk_widget_show (hbox2);
  gtk_container_add (GTK_CONTAINER (alignment2), hbox2);

  image2 = gtk_image_new_from_stock ("gtk-cancel", GTK_ICON_SIZE_BUTTON);
  gtk_widget_show (image2);
  gtk_box_pack_start (GTK_BOX (hbox2), image2, FALSE, FALSE, 0);

  label4 = gtk_label_new_with_mnemonic (_("Cancel"));
  gtk_widget_show (label4);
  gtk_box_pack_start (GTK_BOX (hbox2), label4, FALSE, FALSE, 0);

  button_ok = gtk_button_new ();
  gtk_widget_show (button_ok);
  gtk_container_add (GTK_CONTAINER (hbuttonbox1), button_ok);
  gtk_widget_set_size_request (button_ok, 65, 30);
  GTK_WIDGET_SET_FLAGS (button_ok, GTK_CAN_DEFAULT);

  alignment1 = gtk_alignment_new (0.5, 0.5, 0, 0);
  gtk_widget_show (alignment1);
  gtk_container_add (GTK_CONTAINER (button_ok), alignment1);

  hbox1 = gtk_hbox_new (FALSE, 2);
  gtk_widget_show (hbox1);
  gtk_container_add (GTK_CONTAINER (alignment1), hbox1);

  image1 = gtk_image_new_from_stock ("gtk-ok", GTK_ICON_SIZE_BUTTON);
  gtk_widget_show (image1);
  gtk_box_pack_start (GTK_BOX (hbox1), image1, FALSE, FALSE, 0);

  label3 = gtk_label_new_with_mnemonic (_("OK"));
  gtk_widget_show (label3);
  gtk_box_pack_start (GTK_BOX (hbox1), label3, FALSE, FALSE, 0);

  g_signal_connect((gpointer) download, "destroy", G_CALLBACK(gtk_widget_destroy), NULL);
  g_signal_connect((gpointer) button_cancel, "clicked", G_CALLBACK(on_newdown_buttoncancel_clicked), download);
  g_signal_connect((gpointer) button_ok, "clicked", G_CALLBACK(on_newdown_buttonok_clicked), NULL);

  /* Store pointers to all widgets, for use by lookup_widget(). */
  GLADE_HOOKUP_OBJECT_NO_REF (download, download, "download");
  GLADE_HOOKUP_OBJECT (download, vbox1, "vbox1");
  GLADE_HOOKUP_OBJECT (download, notebook1, "notebook1");
  GLADE_HOOKUP_OBJECT (download, fixed1, "fixed1");
  GLADE_HOOKUP_OBJECT (download, urlentry, "urlentry");
  GLADE_HOOKUP_OBJECT (download, label5, "label5");
  GLADE_HOOKUP_OBJECT (download, label1, "label1");
  GLADE_HOOKUP_OBJECT (download, label2, "label2");
  GLADE_HOOKUP_OBJECT (download, hbuttonbox1, "hbuttonbox1");
  GLADE_HOOKUP_OBJECT (download, button_cancel, "button_cancel");
  GLADE_HOOKUP_OBJECT (download, alignment2, "alignment2");
  GLADE_HOOKUP_OBJECT (download, hbox2, "hbox2");
  GLADE_HOOKUP_OBJECT (download, image2, "image2");
  GLADE_HOOKUP_OBJECT (download, label4, "label4");
  GLADE_HOOKUP_OBJECT (download, button_ok, "button_ok");
  GLADE_HOOKUP_OBJECT (download, alignment1, "alignment1");
  GLADE_HOOKUP_OBJECT (download, hbox1, "hbox1");
  GLADE_HOOKUP_OBJECT (download, image1, "image1");
  GLADE_HOOKUP_OBJECT (download, label3, "label3");

  return download;

}
